package com.riven.bilibilidemo.fragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.activity.RecommendWebActivity;
import com.riven.bilibilidemo.adapter.CommonAdapter;
import com.riven.bilibilidemo.adapter.HeaderAndFooterSupport;
import com.riven.bilibilidemo.adapter.MulitItemTypeSupport;
import com.riven.bilibilidemo.adapter.MulitItemTypeaAdapter;
import com.riven.bilibilidemo.adapter.ViewHolder;
import com.riven.bilibilidemo.application.GlideImageLoader;
import com.riven.bilibilidemo.bean.RecommendBannerInfo;
import com.riven.bilibilidemo.bean.RecommendInfo;
import com.riven.bilibilidemo.interfaces.RequestJson2BeanCallBack;
import com.riven.bilibilidemo.utils.HttpUtil;
import com.riven.bilibilidemo.utils.UIUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static com.riven.bilibilidemo.R.id.swipe_refresh_layout;

/**
 * @author rivenlee
 * @date 2017/3/9 17:36
 */

public class RecommendFragment extends BaseFragment implements MulitItemTypeSupport<RecommendInfo.ResultBean>, SwipeRefreshLayout.OnRefreshListener, OnBannerListener {

    private final String RECOMMEND = "recommend"; //热门焦点
    private final String LIVE = "live";           //正在直播
    private final String BANGUMI = "bangumi_2";     //番剧推荐
    private final String WEBLINK = "weblink";       //话题
    private final String REGION = "region";         //动画区
    private final String ACTIVITY = "activity";     //活动中心

    private final int FLAG1 = 1;
    private final int FLAG2 = 2;
    private final int FLAG3 = 3;
    private final int FLAG4 = 4;
    private final int FLAG5 = 5;
    private final int FLAG6 = 6;

    private RecyclerView recyclerView;
    private String recyclerUrl = "http://app.bilibili.com/x/show/old?platform=android&device=&build=412001";
    private String bannerUrl = "http://app.bilibili.com/x/banner?plat=4&build=411007&channel=bilih5";
    private MulitItemTypeaAdapter mAdapter;

    private int[] icons = new int[] {
            R.drawable.ic_category_t1, R.drawable.ic_category_t3, R.drawable.ic_category_t129,
            R.drawable.ic_category_t4, R.drawable.ic_category_t119,
            R.drawable.ic_category_t160,R.drawable.ic_category_t36,
            R.drawable.ic_category_t155, R.drawable.ic_category_t5,
            R.drawable.ic_category_t11, R.drawable.ic_category_t23
    };
    private int count = 0;

    private SwipeRefreshLayout swipeRefreshLayout;
    private Random random;
    private ArrayList<String> bannerList = new ArrayList<>();
    private Banner banner;
    private List<RecommendBannerInfo.DataBean> bannerDataList;

    @Override
    public int layoutID() {
        return R.layout.fragment_home_recommend;
    }

    @Override
    protected void initViews(View view) {
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(swipe_refresh_layout);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
        swipeRefreshLayout.setOnRefreshListener(this);
        recyclerView = (RecyclerView) view.findViewById(R.id.recommend_recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        random = new Random();

    }

    private void initRecyclerAdapter(List<RecommendInfo.ResultBean> resultList) {
        mAdapter = new MulitItemTypeaAdapter<RecommendInfo.ResultBean>(mContext,resultList,this) {
            @Override
            public void conver(ViewHolder viewHodler, RecommendInfo.ResultBean o) {
                int type = viewHodler.getItemViewType();
                final int i = random.nextInt(50) + 100;
                switch (type){
                    case FLAG1:
                        viewHodler.setText(R.id.item_type_tv,o.getHead().getTitle());
                        CommonAdapter hotAdapter = new CommonAdapter<RecommendInfo.ResultBean.BodyBean>
                                (mContext,R.layout.layout_home_recommend_boby,o.getBody()) {
                            @Override
                            public void conver(ViewHolder viewHodler, RecommendInfo.ResultBean.BodyBean bodyBean) {
//                                LogUtil.newInstance().debug(bodyBean.toString());
//                                viewHodler.setImageForNet(R.id.item_img,bodyBean.getCover())
//                                        .setText(R.id.item_title,bodyBean.getTitle())
//                                        .setText(R.id.item_play,bodyBean.getPlay())
//                                        .setText(R.id.item_review,bodyBean.getDanmaku());
                                viewHodler.setImageForNet(R.id.video_preview,bodyBean.getCover())
                                        .setText(R.id.video_title,bodyBean.getTitle())
                                        .setText(R.id.video_play_num,bodyBean.getPlay())
                                        .setText(R.id.video_review_count,bodyBean.getDanmaku());

                            }
                        };
                        viewHodler.setAdapter(R.id.hot_recyclerView,hotAdapter);
                        break;

                    case FLAG2:
                        viewHodler.setText(R.id.live_count_tv,o.getHead().getCount()+"");
                        CommonAdapter liveAdapter = new CommonAdapter<RecommendInfo.ResultBean.BodyBean>
                                (mContext,R.layout.layout_home_recommend_live,o.getBody()) {
                            @Override
                            public void conver(ViewHolder viewHodler, RecommendInfo.ResultBean.BodyBean bodyBean) {
                                viewHodler.setImageForNet(R.id.video_preview,bodyBean.getCover())
                                        .setText(R.id.video_title,bodyBean.getTitle())
                                        .setText(R.id.item_live_up,bodyBean.getUp())
                                        .setText(R.id.item_live_online,bodyBean.getOnline()+"");

                            }
                        };
                        viewHodler.setAdapter(R.id.hot_recyclerView,liveAdapter)
                             .setText(R.id.item_dynamic,i+"条新动态,点这里刷新");

                        break;

                    case FLAG3:
                        CommonAdapter newDramaAdapter = new CommonAdapter<RecommendInfo.ResultBean.BodyBean>
                                (mContext,R.layout.layout_home_recommend_newdrama,o.getBody()) {
                            @Override
                            public void conver(ViewHolder viewHodler, RecommendInfo.ResultBean.BodyBean bodyBean) {
                                viewHodler.setImageForNet(R.id.video_preview,bodyBean.getCover())
                                        .setText(R.id.video_title,bodyBean.getTitle())
                                        .setText(R.id.item_bangumi_update,bodyBean.getDesc1());

                            }
                        };
                        viewHodler.setAdapter(R.id.hot_recyclerView,newDramaAdapter);
                        break;

                    case FLAG4:
                        viewHodler.setImageForNet(R.id.topic_image,o.getBody().get(0).getCover());
                        break;

                    case FLAG5:
                        switchTypeImg(viewHodler,o.getHead());
//                        viewHodler.setBackgroundRes(R.id.item_type_img,icons[count++%icons.length]);
                        viewHodler.setText(R.id.item_type_tv,o.getHead().getTitle());
                        CommonAdapter normalAdapter = new CommonAdapter<RecommendInfo.ResultBean.BodyBean>
                                (mContext,R.layout.layout_home_recommend_boby,o.getBody()) {
                            @Override
                            public void conver(ViewHolder viewHodler, RecommendInfo.ResultBean.BodyBean bodyBean) {
                                viewHodler.setImageForNet(R.id.video_preview,bodyBean.getCover())
                                .setText(R.id.video_title,bodyBean.getTitle())
                                .setText(R.id.video_play_num,bodyBean.getPlay())
                                .setText(R.id.video_review_count,bodyBean.getDanmaku());
                            }
                        };
                        viewHodler.setAdapter(R.id.hot_recyclerView,normalAdapter)
                                .setText(R.id.item_dynamic,i+"条新动态,点这里刷新");;
                        break;

                    case FLAG6:
                        CommonAdapter centerAdapter = new CommonAdapter<RecommendInfo.ResultBean.BodyBean>
                                (mContext,R.layout.layout_home_recommend_activitycenter,o.getBody()) {
                            @Override
                            public void conver(ViewHolder viewHodler, RecommendInfo.ResultBean.BodyBean bodyBean) {
                                viewHodler.setImageForNet(R.id.video_preview,bodyBean.getCover())
                                        .setText(R.id.video_title,bodyBean.getTitle());
                            }
                        };
                        viewHodler.setLinearAdapter(R.id.hot_recyclerView,centerAdapter);
                        break;
                }

            }
        };
        addRecyclerHeader();
    }

    private void addRecyclerHeader() {
        HeaderAndFooterSupport headerAndFooter = new HeaderAndFooterSupport(mAdapter);
        View view = View.inflate(mContext,R.layout.item_recycler_recommend_banner,null);
        banner = (Banner) view.findViewById(R.id.recycler_recommend_banner);
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(bannerList);
        //设置轮播时间
        banner.setDelayTime(5000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        banner.start();
        //设置Banner点击事件
        banner.setOnBannerListener(this);
        headerAndFooter.addHeaderView(view);
        recyclerView.setAdapter(headerAndFooter);
    }

    private void switchTypeImg(ViewHolder viewHodler, RecommendInfo.ResultBean.HeadBean head) {
        switch (head.getTitle()){
            case "动画区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t1);
                break;
            case "音乐区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t3);
                break;
            case "舞蹈区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t129);
                break;
            case "游戏区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t4);
                break;
            case "鬼畜区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t119);
                break;
            case "生活区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t160);
                break;
            case "科技区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t36);
                break;
            case "时尚区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t155);
                break;
            case "娱乐区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t5);
                break;
            case "电视剧区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t11);
                break;
            case "电影区":
                viewHodler.setBackgroundRes(R.id.item_type_img,R.drawable.ic_category_t23);
                break;
        }
    }

    @Override
    protected void initData() {
        loadDataForNet();
        loadBannerDataForNet();
    }

    private void loadBannerDataForNet() {
        HttpUtil.newInstance().getRequest(bannerUrl, RecommendBannerInfo.class, new RequestJson2BeanCallBack<RecommendBannerInfo>() {
            @Override
            public void onSucces(RecommendBannerInfo recommendBannerInfo) {
                bannerDataList = recommendBannerInfo.getData();
                bannerList.clear();
                for (RecommendBannerInfo.DataBean dataBean : bannerDataList) {
                    bannerList.add(dataBean.getImage());
                }
            }

            @Override
            public void onFail(String error) {

            }
        });

    }

    public void loadDataForNet(){
        HttpUtil.newInstance().getRequest(recyclerUrl, RecommendInfo.class, new RequestJson2BeanCallBack<RecommendInfo>() {
            @Override
            public void onSucces(RecommendInfo recommendInfo) {
                List<RecommendInfo.ResultBean> resultList = recommendInfo.getResult();
                initRecyclerAdapter(resultList);
//                mAdapter.setDataList(resultList);
            }

            @Override
            public void onFail(String error) {
                UIUtils.getToast("无网络连接！");
            }
        });
    }

    @Override
    public int getLayoutID(int ItemType) {
        switch (ItemType){
            case FLAG1:
               return R.layout.item_recycler_recommed_hot;
            case FLAG2:
                return R.layout.item_recycler_recommend_live;
            case FLAG3:
                return R.layout.item_recycler_recommend_newdrama;
            case FLAG4:
                return R.layout.item_recycler_recommend_topic;
            case FLAG5:
                return R.layout.item_recycler_recommend_normal;
            case FLAG6:
                return R.layout.item_recycler_recommend_activitycenter;
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position, RecommendInfo.ResultBean result) {
        if(result.getType().equals(RECOMMEND)){
            return FLAG1;
        }else if(result.getType().equals(LIVE)){
            return FLAG2;
        }else if(result.getType().equals(BANGUMI)){
            return FLAG3;
        }else if(result.getType().equals(WEBLINK)){
            return FLAG4;
        }else if(result.getType().equals(REGION)){
            return FLAG5;
        }else if(result.getType().equals(ACTIVITY)){
            return FLAG6;
        }
        return 0;
    }

    @Override
    public void onRefresh() {
        loadDataForNet();
        loadBannerDataForNet();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void OnBannerClick(int position) {
        RecommendBannerInfo.DataBean dataBean = bannerDataList.get(position);
        Intent intent = new Intent(getActivity(), RecommendWebActivity.class);
        intent.putExtra("title",dataBean.getTitle());
        intent.putExtra("value",dataBean.getValue());
        startActivity(intent);
    }
}
